package biznussoft.metercor.fragments;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TextView;
import biznussoft.metercor.HomeActivity;
import biznussoft.metercor.R;
import biznussoft.metercor.lookup.SitesLookup;
import biznussoft.metercor.model.StockTransferLine;
import biznussoft.metercor.model.StockTransferModel;

import com.salesforce.androidsdk.app.SalesforceSDKManager;
import com.salesforce.androidsdk.rest.RestClient;

public class StockTransferLineDetails  extends Fragment implements OnClickListener{

	private RestClient client;
	private TextView stockTransferNo,stockTransfer,itemId,serialId,lotId,qtrTransferred,additionalInformation,requestedDate;
	private TableLayout viewLayout,editLayout;
	private EditText etQuantityTransferred,etAdditionalInformation;
	public static EditText etStocktransfer,etItemId; 
	StockTransferModel model;
	StockTransferLine mod;
	ArrayList<StockTransferLine> list;
	String id;
	StockTransferLine bean;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.stock_transfer_line,null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		stockTransferNo = (TextView) getView().findViewById(R.id.tvStocktransferno);		
		stockTransfer = (TextView) getView().findViewById(R.id.tvStocktransfer);
		itemId = (TextView) getView().findViewById(R.id.tvitemId);
		serialId = (TextView) getView().findViewById(R.id.tvSerialId);
		lotId = (TextView) getView().findViewById(R.id.tvLotId);
		qtrTransferred = (TextView) getView().findViewById(R.id.tvQuantityTransfered);
		additionalInformation = (TextView)getView().findViewById(R.id.additionalInformation);
		etQuantityTransferred = (EditText) getView().findViewById(R.id.etQuantityTransfered);
		etStocktransfer = (EditText) getView().findViewById(R.id.etStockTransfer);
		etStocktransfer.setOnClickListener(this);
		etItemId = (EditText) getView().findViewById(R.id.etItemId);
		etStocktransfer.setOnClickListener(this);
		etAdditionalInformation = (EditText) getView().findViewById(R.id.etAdditionalInformation);

		editLayout = (TableLayout) getView().findViewById(R.id.ttLayout1);
		viewLayout = (TableLayout) getView().findViewById(R.id.ttLayout2);

		viewLayout.setVisibility(View.VISIBLE);

		client = HomeActivity.mActivity.client;
		

	}

	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu, inflater);


		MenuItem actionItem = menu.add("Logout");                 
		MenuItemCompat.setShowAsAction(actionItem,MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
		actionItem.setIcon(R.drawable.logout);
		MenuItem newItem = menu.add("Edit");                 
		MenuItemCompat.setShowAsAction(newItem,MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
		newItem.setIcon(R.drawable.edit); 


	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if(item.getTitle().equals("Logout")){
			SalesforceSDKManager.getInstance().logout(getActivity());}
		if(item.getTitle().equals("Edit")){
			setHasOptionsMenu(false);
			editStockTransfer();
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	public void stockTransferDetails(int position){
		
		model = StockTransferFragment.stockList.get(position);
		list = StockTransferFragment.stockTransferLineMap.get(model.getId());
		mod = list.get(position);
		stockTransferNo.setText(mod.getName());		
		itemId.setText(mod.getItem());		
		serialId.setText(mod.getSerialId());		
		lotId.setText(mod.getLotId());		
		qtrTransferred.setText(mod.getTransferred());		
		//additionalInformation.setText(mod.get);		
		
	}

	public void newStockTransfer(){
		viewLayout.setVisibility(View.GONE);
		editLayout.setVisibility(View.VISIBLE);
	}
	public void editStockTransfer(){
		viewLayout.setVisibility(View.GONE);
		editLayout.setVisibility(View.VISIBLE);
//		qtrTransferred.setText(bean.getName());
//		etAdditionalInformation.setText(bean.getAdditionalInformation());
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub


		switch (v.getId()) {
		case R.id.sourceLocation:
			SitesLookup lookup = new SitesLookup();
			lookup.getItemLookUp(getActivity(), "");
			break;
		case R.id.destinationLocation:


			break;

		default:
			break;
		}
	
	}

	

	

	/*TextView siteName,truckquantity,t1;
	Button b1,b2;

	GlobalListener listener;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.stock_transfer_details, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		siteName = (TextView) getView().findViewById(R.id.tvSiteName);
		truckquantity = (TextView) getView().findViewById(R.id.truckQuatntity);
		t1 = (TextView) getView().findViewById(R.id.t1);
		b1 = (Button) getView().findViewById(R.id.button1);
		b1.setOnClickListener(this);
		b2 = (Button) getView().findViewById(R.id.button2);
		b2.setOnClickListener(this);
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);

		listener = (GlobalListener) getActivity();

	}
	public void getWarehouseDetails(String  title,String qut) {
		// TODO Auto-generated method stub

		siteName.setText(title);
		truckquantity.setText(qut);

		if(title.contains("Truck")){

			b1.setVisibility(View.GONE);
			b2.setVisibility(View.GONE);
			t1.setVisibility(View.GONE);
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch (v.getId()) {
		case R.id.button1:
			listener.getSignature("nothing");
			break;
		case R.id.button2:
			listener.getSignature("Quantity");
			break;

		default:
			break;
		}

	}*/

}
